/* This file is part of BonQ - A simple HTTP-based messaging server.
 * Copyright (C) 2009 James Cook
 *
 * BonQ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License, version 2, as published by the
 * Free Software Foundation.
 *
 * BonQ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License, version 2,
 * for more details.
 *
 * You should have received a copy of the GNU General Public License, version
 * 2, along with BonQ.  If not, see:
 *
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */
package com.bonkabonka.queue;

import java.util.TreeMap;

/**
 * This queue stores and only delivers messages once their specified delay has
 * passed.  If a delay is not specified, it is delivered immediately.
 *
 * @author jamesc
 */
public class TemporalQueue extends Queue implements QueriedQueue
{
    private final TreeMap<Long, String> messages = new TreeMap<Long, String>();

    /**
     * Add a message to the queue with no pull delay.
     *
     * @param message String containing the message
     * @return null
     */
    @Override
    public String push(String message)
    {
        return push(message, null);
    }

    /**
     * Add a message to the queue with the specified delay.  It is assumed that
     * the query will successfully parse into a Long, if not then the delay is
     * zero.
     *
     * @param message String containing the message
     * @param query String containing the number of seconds to delay
     * @return null
     */
    public String push(String message, String query)
    {
        long delay = 0;
        if(query != null)
        {
            try
            {
                delay = Long.parseLong(query) * 1000;
            }
            catch(NumberFormatException nfe)
            {
                // Swallow the error and leave delay at 0
            }
        }

        long when = System.currentTimeMillis() + delay;
        synchronized(messages)
        {
            messages.put(when, message);
        }

        return null;
    }

    /**
     * Return the next message in the queue, if one is available, but don't
     * remove it.
     *
     * @return String containing the message
     */
    @Override
    public String peek() {
        String message = null;
        long now = System.currentTimeMillis();

        synchronized(messages)
        {
            if(messages.size() > 0)
            {
                Long key = messages.firstKey();
                if(now >= key)
                {
                    message = messages.get(key);
                }
            }
        }

        return message;
    }

    /**
     * Return the next message in the queue, if one is available.
     *
     * @return String containing the message
     */
    @Override
    public String pull() {
        String message = null;
        long now = System.currentTimeMillis();

        synchronized(messages)
        {
            if(messages.size() > 0)
            {
                Long key = messages.firstKey();
                if(now >= key)
                {
                    message = messages.remove(key);
                }
            }
        }

        return message;
    }
}
